<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>=^.^=</title>
<link rel="stylesheet" href="./style.css">
</head>
<body>
<div class="info">
	<p><a href="https://github.com/yomotsu/camera-controls">GitHub repo</a></p>
	<button onclick="moveToStartPoint()">move to the animation start point</button>
	<br>
	<button onclick="pathAnimation.play( 0 )">path animation</button>
	<br>
	<button onclick="cameraControls.normalizeRotations().reset( true )">reset</button>
</div>

<script async src="https://unpkg.com/es-module-shims@2.6.1/dist/es-module-shims.js"></script>
<script type="importmap">
{
	"imports": {
		"three": "https://unpkg.com/three@0.178.0/build/three.module.js",
		"three/addons/": "https://unpkg.com/three@0.178.0/examples/jsm/"
	}
}
</script>
<script type="module">
import * as THREE from 'three';
import gsap from "https://unpkg.com/gsap@3.13.0/index.js";
import CameraControls from './dist/camera-controls.module.js';
CameraControls.install( { THREE: THREE } );

const width = window.innerWidth;
const height = window.innerHeight;
const clock = new THREE.Clock();
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera( 60, width / height, 0.01, 100 );
camera.position.set( 0, 0, 5 );
const renderer = new THREE.WebGLRenderer();
renderer.setSize( width, height );
document.body.appendChild( renderer.domElement );

const cameraControls = new CameraControls( camera, renderer.domElement );

const mesh = new THREE.Mesh(
	new THREE.BoxGeometry( 1, 1, 1 ),
	new THREE.MeshBasicMaterial( { color: 0xff0000, wireframe: true } )
);
scene.add( mesh );

const gridHelper = new THREE.GridHelper( 50, 50 );
gridHelper.position.y = - 1;
scene.add( gridHelper );

const curve = new THREE.CatmullRomCurve3( [
	new THREE.Vector3( - 3,   2, 1 ),
	new THREE.Vector3(   2,   2, 0 ),
	new THREE.Vector3( - 1,   0, 3 ),
	new THREE.Vector3(   2, - 1, 0 ),
] );
const points = curve.getPoints( 50 );
const pathMesh = new THREE.Line(
	new THREE.BufferGeometry().setFromPoints( points ),
	new THREE.LineBasicMaterial( { color: 0x00ffff } )
);
scene.add( pathMesh );

const moveToStartPoint = () => {

	curve.getPoint ( 0, _tmp );
	cameraControls.normalizeRotations().setLookAt( _tmp.x, _tmp.y, _tmp.z, 0, 0, 0, true );

};

const _tmp = new THREE.Vector3();
const animationProgress = { value: 0 };
const pathAnimation = gsap.fromTo(
	animationProgress,
	{
		value: 0,
	},
	{
		value: 1,
		duration: 3,
		overwrite: true,
		paused: true,
		onUpdateParams: [ animationProgress ],
		onUpdate( { value } ) {

			if ( ! this.isActive() ) return;

			curve.getPoint ( value, _tmp );
			const cameraX = _tmp.x;
			const cameraY = _tmp.y;
			const cameraZ = _tmp.z;
			const lookAtX = 0;
			const lookAtY = 0;
			const lookAtZ = 0;

			cameraControls.normalizeRotations().setLookAt(
				cameraX,
				cameraY,
				cameraZ,
				lookAtX,
				lookAtY,
				lookAtZ,
				false, // IMPORTANT! disable cameraControls's transition and leave it to gsap.
			);

		},
		onStart() {

			cameraControls.enabled = false;

		},
		onComplete() {

			cameraControls.enabled = true;

		},
	}
);

renderer.render( scene, camera );

( function anim () {

	const delta = clock.getDelta();
	const elapsed = clock.getElapsedTime();
	const updated = cameraControls.update( delta );

	// if ( elapsed > 30 ) { return; }

	requestAnimationFrame( anim );

	if ( updated ) {

		renderer.render( scene, camera );
		console.log( 'rendered' );

	}

} )();

// make variable available to browser console
globalThis.cameraControls = cameraControls;
globalThis.moveToStartPoint = moveToStartPoint;
globalThis.pathAnimation = pathAnimation;
</script>

</body>
</html>
